﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Model;
using BLL;
using Utility;

public partial class VehicleUI_YearCheck_YearCheckAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Type"] == null)
            { }
            else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_VehicleID.Enabled = false;
                txt_YCTime.Enabled = false;
                txt_FrameNO.Enabled = false;
                txt_ValidTime.Enabled = false;
                btn_Sure.Visible = false;
            }
            else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }


    protected void LoadService()
    {
        int _id = Convert.ToInt32(Request.QueryString["ID"]);
        YearCheck cs = new YearCheckBLL().GetModel(_id);
        if (cs != null)
        {
            txt_VehicleID.Text = cs.VehicleID;
            txt_FrameNO.Text = cs.FrameNO;
            txt_ValidTime.Text = cs.ValidDate.ToString();
            txt_YCTime.Text = cs.YC_Date.ToString();
        }
    }

    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        string _frameNO = txt_FrameNO.Text.Trim();
        bool reExist = new VehicleBLL().Exists(_frameNO);
        if (!reExist)
        {
            UtilityService.Alert(this.Page, "不存在该车辆信息,请重新录入车架号!");
            txt_FrameNO.Focus();
            return;
        }

        if (Request.QueryString["Type"] == null)
        {
            YearCheck cs = new YearCheck();

            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.YC_Date = DateTime.Parse(txt_YCTime.Text.Trim());
            cs.ValidDate =  DateTime.Parse(txt_ValidTime.Text.Trim());
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

            int re = new YearCheckBLL().Add(cs);

            if (re>0)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "YearCheckMgr.aspx");
            }
            else
            {

                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            int _id = Convert.ToInt32(Request.QueryString["ID"]);
            YearCheck cs = new YearCheckBLL().GetModel(_id);
            cs.ID = _id;
            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.YC_Date = DateTime.Parse(txt_YCTime.Text.Trim());
            cs.ValidDate = DateTime.Parse(txt_ValidTime.Text.Trim());

            bool re = new YearCheckBLL().Update(cs);

            if (re)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "修改成功! " + msg, "YearCheckMgr.aspx");
            }
            else
            {
                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }

    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("YearCheckMgr.aspx");
    }
}
